package com.vanthan.xoso;


import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vanthan.xoso.fragment.NavigationDrawerFragment;
import com.vanthan.xoso.jsoup.JsoupLocal;



public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

   
    private NavigationDrawerFragment mNavigationDrawerFragment;

   
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
    	
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        if (!mNavigationDrawerFragment.isDrawerOpen()) {
//            // Only show items in the action bar relevant to this screen
//            // if the drawer is not showing. Otherwise, let the drawer
//            // decide what to show in the action bar.
//            getMenuInflater().inflate(R.menu.main, menu);
//            restoreActionBar();
//            return true;
//        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
//        if (id == R.id.action_settings) {
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }

    
    public static class PlaceholderFragment extends Fragment {
       
        private static final String ARG_SECTION_NUMBER = "section_number";
        Bundle bundle;
        private TextView textViewMien;
        private TextView textViewGiaDacBiet;
        private TextView textViewSoThuNhat;
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        	View rootView =null;
        	int key = getArguments().getInt(ARG_SECTION_NUMBER)-1;
        	switch (key) {
			case 0:
				rootView = inflater.inflate(R.layout.fragment_mien_bac, container, false);
				break;
			case 1:
				rootView = inflater.inflate(R.layout.fragment_mien_trung, container, false);
				break;
			default:
				break;
			}
        	
            return rootView;
        }
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
        	
        	textViewMien = (TextView) view.findViewById(R.id.txt_name_titile);
        	textViewGiaDacBiet = (TextView) view.findViewById(R.id.txt_dac_biet);
        	textViewSoThuNhat = (TextView) view.findViewById(R.id.txt_dac_biet_so_1);
        	String link = "http://xoso.wap.vn/ket-qua-xo-so-mien-bac-xstd.html";
        	Data data = new Data();
        	data.execute(link);
        };
        
        private class Data extends AsyncTask<String, String, Void>{
        	StringBuffer buffer = new StringBuffer();
			@Override
			protected Void doInBackground(String... params) {
				JsoupLocal mJsoup = new JsoupLocal();
				buffer = mJsoup.parserKetQua(params[0]);
				return null;
			}
			
			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				textViewMien.setText(buffer);
			}
        	
        }
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}
